﻿using CIS.DB;
using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("SysConferenceMaster")]
	public class SysConferenceMasterController : BaseDbController
	{
		public ActionResult Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			return View();
		}

		public async Task<JsonCamelCaseResult> GetData(int? page, int? rows, string sort, string order, ConferenceMasterModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = DbContext.ConferenceMaster.AsNoTracking().Where(x => !string.IsNullOrEmpty(x.Status));

			int total = await sql.CountAsync();

			if (!string.IsNullOrWhiteSpace(model.ConferenceName))
			{
				sql = sql.Where(x => x.ConferenceName.Contains(model.ConferenceName) || x.ConferenceNickName.Contains(model.ConferenceName));
			}

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort).ThenByDescending(x => x.UpdateDate).ThenBy(x => x.ConferenceName).Skip(skip).Take(take);				
			}

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.ID,
				ConferenceName = x.ConferenceName,
				OperateTime = x.OperateTime,
				ConferenceStartDate = LoginUser.Language == ConstValues.Language.Cn ? x.ConferenceStartDate.Value.ToString("yyyy-MM-dd") : x.ConferenceStartDate.Value.ToString("MM/dd/yyyy"),
				ConferenceEndDate = LoginUser.Language == ConstValues.Language.Cn ? x.ConferenceEndDate.Value.ToString("yyyy-MM-dd") : x.ConferenceEndDate.Value.ToString("MM/dd/yyyy"),
				OperateLocation = x.OperateLocation,
				CreateTime = LoginUser.Language == ConstValues.Language.Cn ? x.CreateDate.Value.ToString("yyyy-MM-dd HH:mm") : x.CreateDate.Value.ToString("MM/dd/yyyy HH:mm"),
				Status = x.Status,
				ConferenceNickName = x.ConferenceNickName,
				HomePageUrl = x.HomePageUrl,
				IsInHome = x.IsInHome ? Resources.Yes : Resources.No,
				IsRecommend = x.IsRecommend ? Resources.Yes : Resources.No,
				IsDisplay = x.IsDisplay ? Resources.Yes : Resources.No,
				Contact = x.Contact,
				IsDisplayMobile = x.IsDisplayMobile ? Resources.Yes : Resources.No,
				IsDisplayMobileHome = x.IsDisplayMobileHome ? Resources.Yes : Resources.No,
				Sort = x.Sort,
				Creator = x.Creator,
				CreateIP = x.CreateIP
			});

			return JsonCamelCase(new
			{
				Total = total,
				Rows = result,
			},
			JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> Verify(ConferenceMasterModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			var result = await DbContext.ConferenceMaster.FirstOrDefaultAsync(x => x.ID == model.Id);
			if (result == null)
			{
				string errorMessage = Resources.MsgRecordNotExist;
				return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
						JsonRequestBehavior.AllowGet);
			}

			var now = DateTime.Now;
			var time = now.ToString("yyyy-MM-dd");

			if (result.Status != ConstValues.Status.正常)
			{
				result.Status = ConstValues.Status.正常;
				result.Updater = LoginUser.UserName;
				result.UpdateIP = LoginUser.IP;
				result.UpdateDate = now;
				if (result.IsDisplay == false)
				{
					result.IsDisplay = true;
				}

				var notice = new ConferenceNoticeMaster
				{
					ID = result.ID,
					ConferenceName = result.ConferenceName,
					ConferenceNickName = result.ConferenceNickName,
					OperateTime = result.OperateTime,
					OperateLocation = result.OperateLocation,
					ConferenceAddress = result.ConferenceAddress,
					Sponsor = result.Sponsor,
					HomePageUrl = result.HomePageUrl,
					Supporter = result.Supporter,
					Contact = result.Contact,
					ContactPhone = result.ContactPhone,
					ContactMail = result.ContactMail,
					PicForStamp = result.PicForStamp,
					CustomDefinition = result.CustomDefinition == null ? string.Empty : result.CustomDefinition,
					HasAttachment = result.HasAttachment,
				};
				DbContext.ConferenceNoticeMaster.Add(notice);
				await DbContext.SaveChangesAsync();
			}

			var smtpServer = ConstValues.SmtpServer;
			var mailAddress = ConstValues.MailAddress;
			var password = ConstValues.MailPassword;

			var mailServer = await DbContext.SysMailServerMaster.AsNoTracking()
					.OrderBy(x => x.Sort)
					.ThenByDescending(x => x.UpdateDate)
					.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常);

			if (mailServer != null)
			{
				smtpServer = mailServer.MailServer;
				mailAddress = mailServer.Mail;
				password = mailServer.Password;
			}

			var user = await DbContext.SysUserMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.UserName == result.Creator);
					
			MailSender sender = new MailSender(smtpServer);
			MailAddress from = new MailAddress(mailAddress);
			MailAddress to = new MailAddress(user.Mail);

			MailMessage message = Utility.GetMailContent(new MailModel
			{
				MailRemark = ConstValues.MailSubject.会议审核通过,
				Language = result.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn,
				UserName = user.UserName,
				ConferenceId = result.ID,
				ConferenceName = result.ConferenceName
			});
			message.To.Add(to);

			var mailTask = Task.Run(() =>
			{
				sender.SendMail(from, message, password);
			});

			return Json(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> VerifyNotApproved(ConferenceMasterModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			var result = await DbContext.ConferenceMaster.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result != null)
			{
				var now = DateTime.Now;

				if (result.Status != ConstValues.Status.暂停)
				{
					result.Status = ConstValues.Status.暂停;
					result.Updater = LoginUser.UserName;
					result.UpdateIP = LoginUser.IP;
					result.UpdateDate = now;
					if (result.IsDisplay == true)
					{
						result.IsDisplay = false;
					}

					await DbContext.SaveChangesAsync();
				}

				var smtpServer = ConstValues.SmtpServer;
				var mailAddress = ConstValues.MailAddress;
				var password = ConstValues.MailPassword;

				var mailServer = await DbContext.SysMailServerMaster.AsNoTracking()
						.OrderBy(x => x.Sort)
						.ThenByDescending(x => x.UpdateDate)
						.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常);

				if (mailServer != null)
				{
					smtpServer = mailServer.MailServer;
					mailAddress = mailServer.Mail;
					password = mailServer.Password;
				}

				var user = await DbContext.SysUserMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.UserName == result.Creator);

				var registerMail = await DbContext.SysMailMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.Remark == ConstValues.MailSubject.会议审核不通过);
					
				MailSender sender = new MailSender(smtpServer);
				MailAddress from = new MailAddress(mailAddress);
				MailAddress to = new MailAddress(user.Mail);

				MailMessage message = Utility.GetMailContent(new MailModel
				{
					MailRemark = ConstValues.MailSubject.会议审核不通过,
					Language = result.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn,
					ConferenceId = result.ID,
					ConferenceName = result.ConferenceName
				});
				message.To.Add(to);

				var mailTask = Task.Run(() =>
				{
					sender.SendMail(from, message, password);
				});

				return Json(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit), JsonRequestBehavior.AllowGet);
			}
			else
			{
				string errorMessage = Resources.MsgRecordNotExist;
				return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage)
									, JsonRequestBehavior.AllowGet);
			}
		}

		public async Task<JsonCamelCaseResult> GetEditData(string id)
		{
			var data = await DbContext.ConferenceMaster.AsNoTracking().FirstOrDefaultAsync(x => x.ID == id);

			if (data == null)
			{
				string errorMessage = Resources.MsgRecordNotExist;
				return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
						JsonRequestBehavior.AllowGet);
			}

			var result = new
			{
				#region 会议管理
				IsDisplay = data.IsDisplay,
				IsInHome = data.IsInHome,
				IsRecommend = data.IsRecommend,
				IsDisplayMobile = data.IsDisplayMobile,
				IsDisplayMobileHome = data.IsDisplayMobileHome,
				Status = data.Status,
				Sort = data.Sort,
				#endregion

				#region 会议基本信息设置
				ConferenceName = data.ConferenceName,
				ConferenceNickName = data.ConferenceNickName,
				IsInternational = data.IsInternational,
				ConferenceStartDate = data.IsInternational == false ? data.ConferenceStartDate.Value.ToString("yyyy-MM-dd") : data.ConferenceStartDate.Value.ToString("MM/dd/yyyy"),
				ConferenceEndDate = data.IsInternational == false ? data.ConferenceEndDate.Value.ToString("yyyy-MM-dd") : data.ConferenceEndDate.Value.ToString("MM/dd/yyyy"),
				OperateTime = data.OperateTime,
				OperateLocation = data.OperateLocation,
				ConferenceAddress = data.ConferenceAddress,
				Sponsor = data.Sponsor,
				Organizer = data.Organizer,
				CoOrganizer = data.CoOrganizer,
				HomePageUrl = data.HomePageUrl,
				Summary = data.Summary,
				Contact = data.Contact,
				ContactPhone = data.ContactPhone,
				ContactMail = data.ContactMail,
				PicForPC = Path.GetFileName(data.PicForPC),
				PicForMobile = Path.GetFileName(data.PicForMobile),
				Keywords = data.Keywords,
				Description = data.Description,
				Remark = data.Remark,
				#endregion

				#region 注册基本信息设置
				RegisterStartDate = !data.RegisterStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.RegisterStartDate.Value.ToString("yyyy-MM-dd") : data.RegisterStartDate.Value.ToString("MM/dd/yyyy"),
				RegisterEndDate = !data.RegisterEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.RegisterEndDate.Value.ToString("yyyy-MM-dd") : data.RegisterEndDate.Value.ToString("MM/dd/yyyy"),
				PayStartDate = !data.PayStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.PayStartDate.Value.ToString("yyyy-MM-dd") : data.PayStartDate.Value.ToString("MM/dd/yyyy"),
				PayEndDate = !data.PayEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.PayEndDate.Value.ToString("yyyy-MM-dd") : data.PayEndDate.Value.ToString("MM/dd/yyyy"),
				BenefitStartDate = !data.BenefitStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.BenefitStartDate.Value.ToString("yyyy-MM-dd") : data.BenefitStartDate.Value.ToString("MM/dd/yyyy"),
				BenefitEndDate = !data.BenefitEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.BenefitEndDate.Value.ToString("yyyy-MM-dd") : data.BenefitEndDate.Value.ToString("MM/dd/yyyy"),
				AttendenceNotice = data.AttendenceNotice == null ? string.Empty : data.AttendenceNotice,
				#endregion

				#region 投审稿基本信息设置
				ContributeStartDate = !data.ContributeStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.ContributeStartDate.Value.ToString("yyyy-MM-dd") : data.ContributeStartDate.Value.ToString("MM/dd/yyyy"),
				ContributeEndDate = !data.ContributeEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.ContributeEndDate.Value.ToString("yyyy-MM-dd") : data.ContributeEndDate.Value.ToString("MM/dd/yyyy"),
				SubmitRequirement = data.SubmitRequirement == null ? string.Empty : data.SubmitRequirement,
				IsReview = data.IsReview,
				ReviewStartDate = !data.ReviewStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.ReviewStartDate.Value.ToString("yyyy-MM-dd") : data.ReviewStartDate.Value.ToString("MM/dd/yyyy"),
				ReviewEndDate = !data.ReviewEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.ReviewEndDate.Value.ToString("yyyy-MM-dd") : data.ReviewEndDate.Value.ToString("MM/dd/yyyy"),
				FinalStartDate = !data.FinalStartDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.FinalStartDate.Value.ToString("yyyy-MM-dd") : data.FinalStartDate.Value.ToString("MM/dd/yyyy"),
				FinalEndDate = !data.FinalEndDate.HasValue ? string.Empty :
						data.IsInternational == false ? data.FinalEndDate.Value.ToString("yyyy-MM-dd") : data.FinalEndDate.Value.ToString("MM/dd/yyyy"),
				#endregion

				#region 酒店交通基本信息设置
				HotelStartDate = data.HotelStartDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.HotelStartDate.Value.ToString("yyyy-MM-dd") : data.HotelStartDate.Value.ToString("MM/dd/yyyy"),
				HotelEndDate = data.HotelEndDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.HotelEndDate.Value.ToString("yyyy-MM-dd") : data.HotelEndDate.Value.ToString("MM/dd/yyyy"),
				CheckinDate = data.CheckinDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.CheckinDate.Value.ToString("yyyy-MM-dd") : data.CheckinDate.Value.ToString("MM/dd/yyyy"),
				CheckoutDate = data.CheckoutDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.CheckoutDate.Value.ToString("yyyy-MM-dd") : data.CheckoutDate.Value.ToString("MM/dd/yyyy"),
				CheckHint = data.CheckHint,
				TrafficStartDate = data.TrafficStartDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.TrafficStartDate.Value.ToString("yyyy-MM-dd") : data.TrafficStartDate.Value.ToString("MM/dd/yyyy"),
				TrafficEndDate = data.TrafficEndDate.HasValue == false ? string.Empty :
						data.IsInternational == false ? data.TrafficEndDate.Value.ToString("yyyy-MM-dd") : data.TrafficEndDate.Value.ToString("MM/dd/yyyy"),
				TrafficHint = data.TrafficHint,
				#endregion

				#region 会议网站基本信息设置
				ConferenceModel = data.ConferenceModel,
				BannerUrl = Path.GetFileName(data.BannerUrl),
				Introduction = data.Introduction == null ? string.Empty : data.Introduction,
				PicForBackground = Path.GetFileName(data.PicForBackground),
				IsFollowBackground = data.IsFollowBackground,
				OpacityOfPic = data.OpacityOfPic.HasValue ? data.OpacityOfPic : 0,
				ColorForBackground = !string.IsNullOrEmpty(data.ColorForBackground) ? data.ColorForBackground : "#ffffff",
				OpacityOfColor = data.OpacityOfColor.HasValue ? data.OpacityOfColor : 0,
				ColorForConference = !string.IsNullOrEmpty(data.ColorForConference) ? data.ColorForConference : "#c30d23",
				FontColorForMenu = !string.IsNullOrEmpty(data.FontColorForMenu) ? data.FontColorForMenu : "#ffffff",
				ColorForLeft = !string.IsNullOrEmpty(data.ColorForLeft) ? data.ColorForLeft : "#ffffff",
				OpacityOfLeft = data.OpacityOfLeft.HasValue ? data.OpacityOfLeft : 0,
				ColorForCenter = !string.IsNullOrEmpty(data.ColorForCenter) ? data.ColorForCenter : "#ffffff",
				OpacityOfCenter = data.OpacityOfCenter.HasValue ? data.OpacityOfCenter : 0,
				ColorForRight = !string.IsNullOrEmpty(data.ColorForRight) ? data.ColorForRight : "#ffffff",
				OpacityOfRight = data.OpacityOfRight.HasValue ? data.OpacityOfRight : 0,
				ColorForMenu = !string.IsNullOrEmpty(data.ColorForMenu) ? data.ColorForMenu : "#4c4948",
				OpacityOfMenu = data.OpacityOfMenu.HasValue ? data.OpacityOfMenu : 0,
				NewsBackgroundColor = !string.IsNullOrEmpty(data.NewsBackgroundColor) ? data.NewsBackgroundColor : "#ffffff",
				NewsBackgroundOpacity = data.NewsBackgroundOpacity.HasValue ? data.NewsBackgroundOpacity : 0,
				IsShowCountdown = data.IsShowCountdown,
				IsShowShare = data.IsShowShare,
				#endregion

				#region 会议通知设置
				Supporter = data.Supporter,
				HasAttachment = data.HasAttachment,
				CustomDefinition = data.CustomDefinition == null ? string.Empty : data.CustomDefinition,
				PicForStamp = Path.GetFileName(data.PicForStamp),
				#endregion
			};

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<JsonResult> Delete(ConferenceMasterModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			var existAttendence = await DbContext.ConferenceAttendenceInfo.AsNoTracking()
					.Where(x => x.ConferenceId == model.Id).ToListAsync();
			var existContribute = await DbContext.ConferenceContributeInfo.AsNoTracking()
					.Where(x => x.ConferenceId == model.Id).ToListAsync();
			if (existAttendence.Count > 0 || existContribute.Count > 0)
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgUsingCanNotDelete),
						JsonRequestBehavior.AllowGet);
			}

			var result = await DbContext.ConferenceMaster.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result == null)
			{
				string errorMessage = Resources.MsgRecordNotExist;
				return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
						JsonRequestBehavior.AllowGet);
			}

			DbContext.ConferenceMaster.Remove(result);
			await DbContext.SaveChangesAsync();
			return Json(Utility.CreateSuccessJsonResult(Resources.SuccessDelete), JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateInput(false)]
		public async Task<JsonResult> Edit(ConferenceMasterModel model, FormCollection formCollection)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			var result = await DbContext.ConferenceMaster.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result == null)
			{
				string errorMessage = Resources.MsgRecordNotExist;
				return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
						JsonRequestBehavior.AllowGet);
			}

			string nickName = result.ConferenceNickName;
			string folder = Path.Combine(ConstValues.ConferenceImg, nickName);
			string pfolder = Path.Combine(Server.MapPath(ConstValues.ConferenceImg), nickName);

			switch (model.Part)
			{
				case ConstValues.ConferencePart.Display:
					#region 会议管理设置
					result.IsDisplay = formCollection.AllKeys.FirstOrDefault(x => x == "IsDisplay") == null ? false : true;
					result.IsInHome = formCollection.AllKeys.FirstOrDefault(x => x == "IsInHome") == null ? false : true;
					result.IsRecommend = formCollection.AllKeys.FirstOrDefault(x => x == "IsRecommend") == null ? false : true;
					result.IsDisplayMobile = formCollection.AllKeys.FirstOrDefault(x => x == "IsDisplayMobile") == null ? false : true;
					result.IsDisplayMobileHome = formCollection.AllKeys.FirstOrDefault(x => x == "IsDisplayMobileHome") == null ? false : true;
					result.Sort = model.Sort;
					result.Status = model.Status;
					#endregion
					break;
				case ConstValues.ConferencePart.Info:
					#region 会议基本信息设置
					result.ConferenceName = model.ConferenceName?.Trim();
					result.IsInternational = formCollection.AllKeys.FirstOrDefault(x => x == "IsInternational") == null ? false : true;
					result.ConferenceStartDate = model.ConferenceStartDate;
					result.ConferenceEndDate = Utility.ConvertEndDate(model.ConferenceEndDate);
					result.OperateTime = model.OperateTime?.Trim();
					result.OperateLocation = model.OperateLocation?.Trim();
					result.ConferenceAddress = model.ConferenceAddress?.Trim();
					result.Sponsor = model.Sponsor?.Trim();
					result.CoOrganizer = model.CoOrganizer?.Trim();
					result.Organizer = model.Organizer?.Trim();
					result.HomePageUrl = model.HomePageUrl?.Trim();
					result.Summary = model.Summary?.Trim();
					result.Contact = model.Contact?.Trim();
					result.ContactPhone = model.ContactPhone?.Trim();
					result.ContactMail = model.ContactMail?.Trim();

					if (model.PicForPCFile != null)
					{
						if (model.PicForPCFile.ContentLength > ConstValues.PicMaxLength)
						{
							string errorMessage = Resources.MsgPicOverflowError;
							return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
								JsonRequestBehavior.AllowGet);
						}

						string fileName = nickName + ConstValues.strHyphen +
								Path.GetExtension(model.PicForPCFile.FileName).Replace(".", "").ToUpper() + ConstValues.strHyphen +
								DateTime.Now.ToString("yyyy") + ConstValues.strHyphen +
								Utility.GetRandom() + Path.GetExtension(model.PicForPCFile.FileName);
						model.PicForPCFilePath = Path.Combine(folder, fileName);
						var saveFileTask = Task.Run(() =>
						{
							string pFullPath = Path.Combine(pfolder, fileName);
							if (!Directory.Exists(pfolder))
							{
								Directory.CreateDirectory(pfolder);
							}
							model.PicForPCFile.SaveAs(pFullPath);
						});
						Task.WaitAll(saveFileTask);
						result.PicForPC = model.PicForPCFilePath;
					}
					else if (string.IsNullOrEmpty(model.PicForPCFilePath))
					{
						result.PicForPC = string.Empty;
					}

					if (model.PicForMobileFile != null)
					{
						if (model.PicForMobileFile.ContentLength > ConstValues.PicMaxLength)
						{
							string errorMessage = Resources.MsgPicOverflowError;
							return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
								JsonRequestBehavior.AllowGet);
						}

						string fileName = nickName + ConstValues.strHyphen +
								Path.GetExtension(model.PicForMobileFile.FileName).Replace(".", "").ToUpper() +
								ConstValues.strHyphen + DateTime.Now.ToString("yyyy") + ConstValues.strHyphen +
								Utility.GetRandom() + Path.GetExtension(model.PicForMobileFile.FileName);
						model.PicForMobileFilePath = Path.Combine(folder, fileName);
						var saveFileTask = Task.Run(() =>
						{
							string pFullPath = Path.Combine(pfolder, fileName);
							if (!Directory.Exists(pfolder))
							{
								Directory.CreateDirectory(pfolder);
							}
							model.PicForMobileFile.SaveAs(pFullPath);
						});
						Task.WaitAll(saveFileTask);
						result.PicForMobile = model.PicForMobileFilePath;
					}
					else if (string.IsNullOrEmpty(model.PicForMobileFilePath))
					{
						result.PicForMobile = string.Empty;
					}

					result.Keywords = model.Keywords;
					result.Description = model.Description;
					result.Remark = model.Remark?.Trim();
					#endregion
					break;
				case ConstValues.ConferencePart.Attendence:
					#region 注册基本信息设置
					result.RegisterStartDate = model.RegisterStartDate;
					result.RegisterEndDate = Utility.ConvertEndDate(model.RegisterEndDate);
					result.PayStartDate = model.PayStartDate;
					result.PayEndDate = Utility.ConvertEndDate(model.PayEndDate);
					result.BenefitStartDate = model.BenefitStartDate;
					result.BenefitEndDate = Utility.ConvertEndDate(model.BenefitEndDate);
					result.AttendenceNotice = model.AttendenceNotice == null ? string.Empty : model.AttendenceNotice.Trim();
					#endregion
					break;
				case ConstValues.ConferencePart.Contribute:
					#region 投审稿基本信息设置
					result.ContributeStartDate = model.ContributeStartDate;
					result.ContributeEndDate = Utility.ConvertEndDate(model.ContributeEndDate);
					result.SubmitRequirement = model.SubmitRequirement == null ? string.Empty : model.SubmitRequirement.Trim();

					var review = model.IsReview == 2 ? true : false;
					if (result.IsReview != review)
					{
						var exist = await DbContext.ReviewInfo.AsNoTracking()
								.Where(x => x.ConferenceId == model.Id)
								.ToListAsync();
						if (exist.Count > 0)
						{
							return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgReviewHasStart),
									JsonRequestBehavior.AllowGet);
						}
					}
					result.IsReview = review;

					result.ReviewStartDate = model.ReviewStartDate;
					result.ReviewEndDate = Utility.ConvertEndDate(model.ReviewEndDate);
					result.FinalStartDate = model.FinalStartDate;
					result.FinalEndDate = Utility.ConvertEndDate(model.FinalEndDate);
					#endregion
					break;
				case ConstValues.ConferencePart.Booking:
					#region 酒店交通基本信息设置
					result.HotelStartDate = model.HotelStartDate;
					result.HotelEndDate = Utility.ConvertEndDate(model.HotelEndDate);
					result.CheckinDate = model.CheckinDate;
					result.CheckoutDate = Utility.ConvertEndDate(model.CheckoutDate);
					result.CheckHint = model.CheckHint;
					result.TrafficStartDate = model.TrafficStartDate;
					result.TrafficEndDate = Utility.ConvertEndDate(model.TrafficEndDate);
					result.TrafficHint = model.TrafficHint;
					#endregion
					break;
				case ConstValues.ConferencePart.Website:
					#region 会议网站基本信息设置
					result.ConferenceModel = model.ConferenceModel;

					if (model.BannerUrlFile != null)
					{
						if (model.BannerUrlFile.ContentLength > ConstValues.PicMaxLength)
						{
							string errorMessage = Resources.MsgPicOverflowError;
							return JsonCamelCase(Utility.CreateFailJsonResult(errorMessage),
								JsonRequestBehavior.AllowGet);
						}

						string fileName = nickName + ConstValues.strHyphen +
								Path.GetExtension(model.BannerUrlFile.FileName).Replace(".", "").ToUpper() + ConstValues.strHyphen +
								DateTime.Now.ToString("yyyy") + ConstValues.strHyphen +
								Utility.GetRandom() + Path.GetExtension(model.BannerUrlFile.FileName);
						model.BannerUrlFilePath = Path.Combine(folder, fileName);
						var saveFileTask = Task.Run(() =>
						{
							string pFullPath = Path.Combine(pfolder, fileName);
							if (!Directory.Exists(pfolder))
							{
								Directory.CreateDirectory(pfolder);
							}
							model.BannerUrlFile.SaveAs(pFullPath);
						});
						Task.WaitAll(saveFileTask);
						result.BannerUrl = model.BannerUrlFilePath;
					}
					else if (string.IsNullOrEmpty(model.BannerUrlFilePath))
					{
						result.BannerUrl = string.Empty;
					}

					result.Introduction = model.Introduction == null ? string.Empty : model.Introduction.Trim();

					if (model.PicForBackgroundFile != null)
					{
						string fileName = nickName + ConstValues.strHyphen +
								Path.GetExtension(model.PicForBackgroundFile.FileName).Replace(".", "").ToUpper() + ConstValues.strHyphen +
								DateTime.Now.ToString("yyyy") + ConstValues.strHyphen +
								Utility.GetRandom() + Path.GetExtension(model.PicForBackgroundFile.FileName);
						model.PicForBackgroundPath = Path.Combine(folder, fileName);
						var saveFileTask = Task.Run(() =>
						{
							string pFullPath = Path.Combine(pfolder, fileName);
							if (!Directory.Exists(pfolder))
							{
								Directory.CreateDirectory(pfolder);
							}
							model.PicForBackgroundFile.SaveAs(pFullPath);
						});
						Task.WaitAll(saveFileTask);
						result.PicForBackground = model.PicForBackgroundPath;
					}
					else if (string.IsNullOrEmpty(model.PicForBackgroundPath))
					{
						result.PicForBackground = string.Empty;
					}

					result.IsFollowBackground = formCollection.AllKeys.FirstOrDefault(x => x == "IsFollowBackground") == null ? false : true;
					result.OpacityOfPic = model.OpacityOfPic;
					result.ColorForBackground = model.ColorForBackground;
					result.OpacityOfColor = model.OpacityOfColor;
					result.ColorForConference = model.ColorForConference;
					result.ColorForMenu = model.ColorForMenu;
					result.OpacityOfMenu = model.OpacityOfMenu;
					result.FontColorForMenu = model.FontColorForMenu;
					result.ColorForLeft = model.ColorForLeft;
					result.OpacityOfLeft = model.OpacityOfLeft;
					result.ColorForCenter = model.ColorForCenter;
					result.OpacityOfCenter = model.OpacityOfCenter;
					result.ColorForRight = model.ColorForRight;
					result.OpacityOfRight = model.OpacityOfRight;
					result.NewsBackgroundColor = model.NewsBackgroundColor;
					result.NewsBackgroundOpacity = model.NewsBackgroundOpacity;
					result.IsShowCountdown = formCollection.AllKeys.FirstOrDefault(x => x == "IsShowCountdown") == null ? false : true;
					result.IsShowShare = formCollection.AllKeys.FirstOrDefault(x => x == "IsShowShare") == null ? false : true;
					#endregion
					break;
			}

			result.Updater = LoginUser.UserName;
			result.UpdateIP = LoginUser.IP;
			result.UpdateDate = DateTime.Now;
			await DbContext.SaveChangesAsync();

			return Json(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit), JsonRequestBehavior.AllowGet);
		}
	}
}